package cn.edu.xcu.ckl.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.xcu.ckl.R;
import cn.edu.xcu.ckl.entity.OrderInfo;
import cn.edu.xcu.ckl.utils.GlideEngine;

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.MyHolder> {
    private List<OrderInfo> mOrderList = new ArrayList<>();
    private Context mContext;

    public OrderListAdapter(Context context) {
        mContext = context;
    }

    public void setListData(List<OrderInfo> list) {
        if (list == null) {
            mOrderList.clear();
        } else {
            this.mOrderList = list;
        }
        //这句话不能少
        notifyDataSetChanged();

    }

    public List<OrderInfo> getOrderList() {
        return mOrderList;
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_list_item, null);
        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        //绑定数据
        OrderInfo orderInfo = mOrderList.get(position);
        //设置数据
        holder.product_count.setText("x" + orderInfo.getProduct_count());
        GlideEngine.createGlideEngine().loadImage(mContext,orderInfo.getProduct_img(),holder.product_img);
        holder.product_title.setText(orderInfo.getProduct_title());
        holder.product_price.setText(orderInfo.getProduct_price() + "");
        //长按删除事件
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (null != mOnItemClickListener) {
                    mOnItemClickListener.onItemClick(orderInfo, position);
                }
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return mOrderList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        ImageView product_img;
        TextView product_title;
        TextView product_price;
        TextView product_count;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            product_img = itemView.findViewById(R.id.product_img);
            product_title = itemView.findViewById(R.id.product_title);
            product_price = itemView.findViewById(R.id.product_price);
            product_count = itemView.findViewById(R.id.product_count);

        }
    }


    private onItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface onItemClickListener {
        void onItemClick(OrderInfo orderInfo, int position);
    }
}
